package com.erdos.upc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.erdos.common.exception.AbsentParamtersException;
import com.erdos.upc.dao.DataPrivilegeDao;
import com.erdos.upc.entity.BusinessPrivilege;
import com.erdos.upc.entity.ControlMode;
import com.erdos.upc.service.BusinessPrivilegeService;

public class BusinessPrivilegeServiceImpl implements BusinessPrivilegeService {
	
	private DataPrivilegeDao dataPrivilegeDao;
	//private SecretRoleDao secretRoleDao;
	/* (non-Javadoc)
	 * @see com.hp.upc.service.BusinessPrivilegeService#checkUserPrivilege(int, java.lang.String, java.lang.String, java.lang.String)
	 */
	public ControlMode checkUserPrivilege(Long userId, String facilityId,
			String category, String businessId) throws AbsentParamtersException{
		ControlMode controlMode = new ControlMode();
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(category)) {
			params.put("category", category);
		} else {
			throw new AbsentParamtersException("category");
		}
		if(StringUtils.isNotEmpty(businessId)) {
			params.put("businessId", businessId);
		} else {
			throw new AbsentParamtersException("instanceId");
		}
		boolean existFlag = this.dataPrivilegeDao.findPrivilegeExist(params);
		if(!existFlag) {
			//若指定业务对象未做权限控制，则返回全部权限
			controlMode.setControlCode(ControlMode.getAllPrivilege());
		} else {
			//若用户无指定业务对象权限，则返回ControlCode为0的controlMode对象
			int newControlCode = 0;
			controlMode.setControlCode(0);
			if(userId==null) {
				throw new AbsentParamtersException("userId");
			}
			params.put("userId", userId);
			if(StringUtils.isNotEmpty(facilityId)) {
				params.put("facilityId", facilityId);
			}
			List<BusinessPrivilege> userBusinessPrivileges = this.dataPrivilegeDao.findByUserParams(params);
		    if(userBusinessPrivileges!=null&&userBusinessPrivileges.size()>0) {
				for(BusinessPrivilege businessPrivilege:userBusinessPrivileges) {
					newControlCode =newControlCode | businessPrivilege.getControlMode().getControlCode();					
				}
			}
		    controlMode.setControlCode(newControlCode);
		}
		return controlMode;
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.service.BusinessPrivilegeService#findBusinessPrivilegeByUser(int)
	 */
	public List<BusinessPrivilege> findBusinessPrivilegeByUser(Long userId){
		return this.findBusinessPrivilegeByUserAndCategory(userId,null,null);
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.service.BusinessPrivilegeService#findBusinessPrivilegeByUserAndCategory(int, java.lang.String, java.lang.String)
	 */
	public List<BusinessPrivilege> findBusinessPrivilegeByUserAndCategory(
			Long userId, String facilityId, String category) throws AbsentParamtersException{
		List<BusinessPrivilege> businessPrivileges= new ArrayList<BusinessPrivilege>();
		Map<String, Object> params = new HashMap<String, Object>();
		if(userId != null) {
			params.put("userId", userId);
		} else {
			throw new AbsentParamtersException("userId");
		}
		if(StringUtils.isNotEmpty(facilityId)) {
			params.put("facilityId", facilityId);
		}
		if(StringUtils.isNotEmpty(category)) {
			params.put("category", category);
		}
		List<BusinessPrivilege> userBusinessPrivileges = this.dataPrivilegeDao.findByUserParams(params);
		if(userBusinessPrivileges!=null&&userBusinessPrivileges.size()>0) {
			for(BusinessPrivilege businessPrivilege:userBusinessPrivileges) {
				int index = businessPrivileges.indexOf(businessPrivilege);
				//若用户的不同角色具有相同业务对象的权限，做合并
				if(index<0) {
					businessPrivileges.add(businessPrivilege);
				} else {
					int newControlCode = (userBusinessPrivileges.get(index).getControlMode().getControlCode()) | (businessPrivilege.getControlMode().getControlCode()) ;
					businessPrivileges.get(index).getControlMode().setControlCode(newControlCode);
				}
			}			
		}
	    return businessPrivileges;
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.service.BusinessPrivilegeService#findIDByUserAndCategory(int, java.lang.String, java.lang.String)
	 */
	public List<String> findIDByUserAndCategory(Long userId, String facilityId,
			String category) throws AbsentParamtersException{
		List<String> businessIDs= new ArrayList<String>();
		Map<String, Object> params = new HashMap<String, Object>();
		if(userId != null) {
			params.put("userId", userId);
		} else {
			throw new AbsentParamtersException("userId");
		}
		if(StringUtils.isNotEmpty(facilityId)) {
			params.put("facilityId", facilityId);
		}
		if(StringUtils.isNotEmpty(category)) {
			params.put("category", category);
		}
		List<BusinessPrivilege> userBusinessPrivileges = this.dataPrivilegeDao.findByUserParams(params);
		if(userBusinessPrivileges!=null&&userBusinessPrivileges.size()>0) {
			for(BusinessPrivilege businessPrivilege:userBusinessPrivileges) {
				int index = businessIDs.indexOf(businessPrivilege.getBusinessID());
				//若用户的不同角色具有相同业务对象的权限，做合并
				if(index<0) {
					businessIDs.add(businessPrivilege.getBusinessID());
				}
			}			
		}
	    return businessIDs;
	}

	public DataPrivilegeDao getDataPrivilegeDao() {
		return dataPrivilegeDao;
	}

	public void setDataPrivilegeDao(DataPrivilegeDao dataPrivilegeDao) {
		this.dataPrivilegeDao = dataPrivilegeDao;
	}

/*	public SecretRoleDao getSecretRoleDao() {
		return secretRoleDao;
	}

	public void setSecretRoleDao(SecretRoleDao secretRoleDao) {
		this.secretRoleDao = secretRoleDao;
	}*/
	
	
}
